IF exists(SELECT * FROM dbo.sysobjects WHERE name='WEB_llnPerKlasOud' AND xtype='P') 
BEGIN
	DROP PROCEDURE WEB_llnPerKlasOud
END
GO

create procedure WEB_llnPerKlasOud
as
set nocount on

create table #hulptabel(tekst varchar(500))

--De hoofding
insert into #hulptabel(tekst)
values('<?xml version="1.0"  encoding="ISO-8859-1" ?>')
insert into #hulptabel(tekst)
values('<leerlingen>')

--De klassen
declare @klas varchar(5)
declare klasCursor cursor local for
  select distinct klas from leerlingen
  order by klas
for read only

open klasCursor
fetch next from klascursor into @klas
while (@@fetch_status=0  )
    begin
	insert into #hulptabel(tekst)
	values('<klas klasNaam = "' + @klas + '">')

	insert into #hulptabel(tekst)
	select '<lln iNr="' + inschrijvingsNr + '"><naam>'  
	 + isNull(voornaam,'') + ' ' + isNull(achternaam,'') 
	 + '</naam></lln>'
	from leerlingen where klas = @klas
	order by klasNr

	insert into #hulptabel(tekst)
	values('</klas>')
	fetch next from klascursor into @klas
    end
close klasCursor

insert into #hulptabel(tekst)
values('</leerlingen>')

select * from #hulptabel